<?php
/**
 * Author: zsx
 * Date: 2022/9/14
 * Time: 12:17
 * 每个月1号 晚上00:00分执行一次
 * 复制一份仓库表数据到
 */
date_default_timezone_set('PRC');
// 链接数据
$conn = mysqli_connect('localhost', 'root', 'root', 'huiyi');
$month = date('Y-m-d', strtotime('-1 day'));
if ($conn) {
  $data = [];
  $sql = "select * from house";
  $result = mysqli_query($conn, $sql);
  $sql1 = "insert into house_statistics(hid,date,amoumt,wamoumt,oldamoumt,oldwamoumt,badamoumt,nowcost) VALUES ";
  while($row = mysqli_fetch_assoc($result))
  {
    $data[] = $row;
    $amoumt = $row['amoumt'] ? $row['amoumt'] : 0;
    $wamoumt = $row['wamoumt'] ? $row['wamoumt'] : 0;
    $oldamoumt = $row['oldamoumt'] ? $row['oldamoumt'] : 0;
    $oldwamoumt = $row['oldwamoumt'] ? $row['oldwamoumt'] : 0;
    $badamoumt = $row['badamoumt'] ? $row['badamoumt'] : 0;
    $nowcost = $row['nowcost'] ? $row['nowcost'] : 0;
    $sql1 .= "({$row['id']},'{$month}',{$amoumt},{$wamoumt},{$oldamoumt},{$oldwamoumt},{$badamoumt},{$nowcost}),";
  }
  if ($sql1 && $result) {
    $sql1 = substr($sql1, 0, strlen($sql1)-1).';';
    mysqli_query($conn, $sql1);
  }
  mysqli_close($conn);
}
